package main

import (
	"fmt"
	"sort"
)

func main()  {
	
	var a map[string]string
	//再使用map之前，必须使用make给map分配空间
	a = make(map[string]string,10)
	a["1"] = "first"
	fmt.Println(a)

	//
	cities := make(map[string]string)
	cities["2"] = "second"
	fmt.Println(cities)

	//
	var h map[string]string = map[string]string{
		"3" : "third",
	}
	h["4"] = "forth"
	delete(h,"5")
	delete(h,"4")
	fmt.Println(h)

	val,find := h["3"]
	if find {
		fmt.Println(val)
	}

	for key,value := range h {
		fmt.Printf("%v,%v\n",key,value)
	}

	fmt.Println(len(h))  //map长度
	
	//通过map切片动态增加map
	var slice []map[string]string
	slice = make([]map[string]string,1)

	if slice[0] == nil {
		slice[0] = make(map[string]string)
		slice[0]["name"] = "1"
		slice[0]["age"] = "2"
	}

	fmt.Println(slice)

	newMap := make(map[string]string)
	newMap["name"] = "3"
	newMap["age"] = "4"
	slice = append(slice,newMap)

	fmt.Println(slice)

	//对map进行排序输出 先slice切片key，对切片进行排序，用切片key取值
	var sortMap map[int]int = make(map[int]int)
	sortMap[10] = 100
	sortMap[1] = 13
	sortMap[4] = 56
	sortMap[8] = 90
	fmt.Println(sortMap)

	var keys []int
	for k,_ := range sortMap {
		keys = append(keys,k)
	}
	
	sort.Ints(keys)
	fmt.Println(keys)

	for _,k := range keys {
		fmt.Printf("key= %v,value= %v\n",k,sortMap[k])
	}
}

